/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.actions;

import controler.formbeans.ContactUsMsgBean;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.logic.db.CommonWSFunctionality;
import model.logic.nondb.CommonMethods;
import model.pojo.ContactUsMsg;
import model.pojo.Settings;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Maha
 */
public class ContactUSMsgAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ContactUsMsgBean beanData = ((ContactUsMsgBean) form);
        ContactUsMsg msg = new ContactUsMsg();
        msg.setMsgBody(beanData.getMsg());
        msg.setMsgDate(new Date());
        msg.setMsgSubject(beanData.getSubject());
        msg.setSenderMail(beanData.getEmail());
        msg.setSenderName(beanData.getSenderName());
        msg.setSenderPhone(beanData.getPhone());
        CommonWSFunctionality callInsertComment = new CommonWSFunctionality();
        int insertComment = callInsertComment.insertComment(msg);
        //TODO must be handled
        if (insertComment == 1) {
            CommonWSFunctionality f = new CommonWSFunctionality();
            Settings settings = f.getSettings();
            String msgBody = "You got a new notification from  " + msg.getSenderName() + " <br/>Please Login to your account to review the message, <br/><b>GlobiSoft Team</b>";
            CommonMethods.sendMail(settings.getSmtpUser(), settings.getSmtpPassword(), settings.getSmtpName(), settings.getSmtpUser(), settings.getContactEmails(), "You Got a Notification", msgBody);

            ((ContactUsMsgBean) form).setConfirm("Your Message was  sent Successfuly.");


        } else {
            ((ContactUsMsgBean) form).setConfirm("Sorry, Your Message Can't be sent at a moment.");

        }

        return mapping.findForward(
                "contactus");
    }
}
